﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace TuongLai_Schools
{
    public class DbConnection
    {
        private static SqlConnection _cnn = new SqlConnection(clsCommon.sConnectionString);
        public static SqlConnection SqlConnection
        {
            get
            {
                return _cnn;
            }
            set
            {
                _cnn = value;
            }
        }

        public static void Open()
        {
            _cnn.Open();
        }

        public static void Close()
        {
            _cnn.Close();
        }
    }

    public class DbAccess
    {
        SqlCommand cmd;
        #region Open and Close Connection
        private void Open()
        {
            DbConnection.Open();
        }
        private void Close()
        {
            DbConnection.Close();
        }
        #endregion

        #region "Create and Add parameter for SqlCommand"

        public void CreateNewSqlCommand_Proc()
        {
            cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = DbConnection.SqlConnection;
        }
        public void CreateNewSqlCommand_Text()
        {
            cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;
            cmd.Connection = DbConnection.SqlConnection;
        }
        public void AddParameter(string paramName, object value)
        {
            SqlParameter param = new SqlParameter();
            param.ParameterName = paramName;
            param.Value = value;
            cmd.Parameters.Add(param);
        }
        #endregion

        #region Execute sqlComman

        public int ExecuteNonQuery(string sProcName)
        {
            try
            {
                cmd.CommandText = sProcName;
                this.Open();
                return cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                this.Close();
            }
        }

        public SqlDataReader ExecuteReader(string sProcName)
        {
            try
            {
                cmd.CommandText = sProcName;
                this.Open();
                return cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable ExecuteDataTable(string sProcName)
        {
            DataTable dt = new DataTable();
            try
            {
                SqlDataAdapter da = new SqlDataAdapter();
                cmd.CommandText = sProcName;
                da.SelectCommand = cmd;
                da.Fill(dt);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return dt;
        }
        #endregion
    }
}
